package com.ttbj.owner.component;

import java.util.ArrayList;
import java.util.List;
import java.util.TooManyListenersException;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import com.ttbj.core.dao.redis.IRedisValueBaseDao;
import com.ttbj.product.bean.dto.OwnerDto;

@Component
public class OwnerCache {
	private static final Logger logger = LoggerFactory.getLogger(OwnerCache.class);
			
	
	public static final String SENDER_KEY =  "_^&^_sender_^&^_ttbj";
	public static final String RECIVER_KEY=	 "_^&^_reciver_^&^_ttbj";
 

	@Autowired
	private IRedisValueBaseDao<ArrayList<OwnerDto>> redisValueDao = null;

	/**
	 * 查询所有寄件人或是收件人信息
	 * @param openId
	 * @param isSender
	 * @return
	 */
	public ArrayList<OwnerDto> getListByCache(String openId,Integer isSender) {
		return redisValueDao.get(key(openId, isSender));
	}
	
	/**
	 * 精确查询寄件人或收件人信息
	 * @param openId
	 * @param isSender
	 * @param id
	 * @return
	 */
	public OwnerDto getValueByCache(String openId,Integer isSender, Long id){
		ArrayList<OwnerDto> list = getListByCache(openId, isSender);
		if(null== list || list.isEmpty()) {
			return null;
		}
	    List<OwnerDto> collect = list.stream().filter(x->x.getOwnerId() != null).filter(x->x.getOwnerId().equals(id)).collect(Collectors.toList());
		if(null == collect || collect.isEmpty()){
			return null;
		}
		return collect.get(0);
	}
	
	
	
	/**
	 * 添加或是修改资料
	 * @param openId
	 * @param dto
	 * @param isSender
	 */
	public  void setValueByCache(String openId,OwnerDto dto ,Integer isSender){
		ArrayList<OwnerDto> list = getListByCache(openId, isSender);
		if(null == list || list.isEmpty()){
			logger.info("缓存中没有寄件人或是收件人信息,创建缓存信息");
			list = new ArrayList<OwnerDto>();
		}
		list.add(dto);
		setListByCache(openId, list, isSender);
	}
	
	/**
	 * 批量添加或修改
	 * @param openId
	 * @param list
	 * @param isSender
	 */
	public void setListByCache(String openId,ArrayList<OwnerDto> list,Integer isSender){
		if(null == list || list.isEmpty()){
			logger.info("寄件人或是收件人信息为空，添加缓存失败");
			return;
		}
		redisValueDao.set(key(openId, isSender), list);
	}
	
	/**
	 * 删除用户资料
	 * @param openId
	 * @param id
	 * @param isSender
	 */
	public void delValueByCache(String openId,Long id,Integer isSender){
		ArrayList<OwnerDto> list = getListByCache(openId, isSender);
		if(null == list || list.isEmpty()){
			logger.info("缓存中没有寄件人或是收件人信息,创建缓存信息");
			return ;
		}
		list.stream().filter(x->!x.getOwnerId().equals(id));
		setListByCache(openId, list, isSender);
	}
	
	private String key(String openId ,Integer isSender){
		StringBuffer sb = new StringBuffer(openId);
		return isSender == 0 ? sb.append(SENDER_KEY).toString() : sb.append(RECIVER_KEY).toString();
	}
	
	

}
